{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Configuration\n",
    "\n",
    "Define settings for remote services, create `appsettings.Development.json` using `appsettings.json` as template.\n",
    "\n",
    "If you want to use this script to insert sample data into your production deployment (after `azd up`) you need to manually give yourself\n",
    "- `Cognitive Services OpenAI Contributor` rights on the `Azure OpenAI` resource\n",
    "- `Search Index Data Contributor` right on the `Azure AI Search` resource.\n",
    "\n",
    "Please execute `azd auth login` after that to have access to these Azure resources."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Install and Import Required Packages\n",
    "Install and import the necessary packages using NuGet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Azure.Identity, 1.13.1</span></li><li><span>Azure.Search.Documents, 11.6.0</span></li><li><span>CsvHelper, 33.0.1</span></li><li><span>Microsoft.Extensions.Configuration, 9.0.0</span></li><li><span>Microsoft.Extensions.Configuration.Binder, 9.0.0</span></li><li><span>Microsoft.Extensions.Configuration.EnvironmentVariables, 9.0.0</span></li><li><span>Microsoft.Extensions.Configuration.UserSecrets, 9.0.0</span></li><li><span>Microsoft.SemanticKernel, 1.33.0</span></li><li><span>Microsoft.SemanticKernel.Connectors.AzureAISearch, 1.33.0-preview</span></li><li><span>Microsoft.SemanticKernel.Plugins.Core, 1.33.0-alpha</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: Azure.Identity, 1.13.1\"\n",
    "#r \"nuget: CsvHelper, 33.0.1\"\n",
    "#r \"nuget: Microsoft.Extensions.Configuration, 9.0.0\"\n",
    "#r \"nuget: Microsoft.Extensions.Configuration.Binder, 9.0.0\"\n",
    "#r \"nuget: Microsoft.Extensions.Configuration.UserSecrets, 9.0.0\"\n",
    "#r \"nuget: Microsoft.Extensions.Configuration.EnvironmentVariables, 9.0.0\"\n",
    "#r \"nuget: Azure.Search.Documents, 11.6.0\"\n",
    "#r \"nuget: Microsoft.SemanticKernel, 1.33.0\"\n",
    "#r \"nuget: Microsoft.SemanticKernel.Plugins.Core, 1.33.0-alpha\"\n",
    "#r \"nuget: Microsoft.SemanticKernel.Connectors.AzureAISearch, 1.33.0-preview\"\n",
    "\n",
    "// Import the necessary libraries\n",
    "using System;\n",
    "using System.Threading;\n",
    "using System.Net;\n",
    "using System.ComponentModel;\n",
    "using System.Diagnostics;\n",
    "using System.IO;\n",
    "using System.Text.Json;\n",
    "using System.Threading.Tasks;\n",
    "using System.Globalization;\n",
    "using Azure.Identity;\n",
    "using CsvHelper;\n",
    "using Microsoft.Extensions.Configuration;\n",
    "using Microsoft.Extensions.DependencyInjection;\n",
    "using Microsoft.Extensions.VectorData;\n",
    "using Azure.Search.Documents.Indexes;\n",
    "using Microsoft.SemanticKernel;\n",
    "using Microsoft.SemanticKernel.Connectors.AzureAISearch;\n",
    "using Microsoft.SemanticKernel.Connectors.AzureOpenAI;\n",
    "using Microsoft.SemanticKernel.Data;\n",
    "using Microsoft.SemanticKernel.Embeddings;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import Sample Products\n",
    "\n",
    "Imports all sample products from `products.csv` and generates for them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// The Agent Framework is experimental and requires warning suppression\n",
    "#pragma warning disable CA2007, IDE1006, SKEXP0050, SKEXP0001, SKEXP0110, SKEXP0010, OPENAI001\n",
    "\n",
    "var configBuilder = new ConfigurationBuilder()\n",
    "    .SetBasePath(Directory.GetCurrentDirectory())\n",
    "    .AddJsonFile(\"./appsettings.json\", optional: false)\n",
    "    .AddJsonFile(\"./appsettings.Development.json\", optional: false);   \n",
    "var configuration = configBuilder.Build();\n",
    "\n",
    "internal sealed class ProductDataModel\n",
    "{\n",
    "    [VectorStoreRecordKey]\n",
    "    public string Key { get; set; }\n",
    "\n",
    "    [VectorStoreRecordData]\n",
    "    [TextSearchResultName]\n",
    "    public string Name { get; set; }\n",
    "\n",
    "    [VectorStoreRecordData]\n",
    "    [TextSearchResultValue]\n",
    "    public string Content { get; set; }\n",
    "\n",
    "    [VectorStoreRecordVector(3072)]\n",
    "    public ReadOnlyMemory<float> Embedding { get; set; }\n",
    "}\n",
    "\n",
    "var collectionName = \"products\";\n",
    "\n",
    "var searchIndexClient = new SearchIndexClient(new Uri(configuration[\"AzureAISearchEndpoint\"]), new AzureDeveloperCliCredential());\n",
    "var vectorStore = new AzureAISearchVectorStore(searchIndexClient);\n",
    "\n",
    "IVectorStoreRecordCollection<string, ProductDataModel> recordCollection = vectorStore.GetCollection<string, ProductDataModel>(collectionName);\n",
    "await recordCollection.CreateCollectionIfNotExistsAsync();\n",
    "\n",
    "var textEmbeddingGeneration = new AzureOpenAITextEmbeddingGenerationService(\n",
    "    configuration[\"EmbeddingModelDeployment\"],\n",
    "    configuration[\"AzureOpenAIEndpoint\"],\n",
    "    new AzureDeveloperCliCredential());\n",
    "\n",
    "\n",
    "using (var reader = new StreamReader(\"./products.csv\"))\n",
    "using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))\n",
    "{\n",
    "    var records = csv.GetRecords<dynamic>().ToList();\n",
    "\n",
    "    foreach (var record in records)\n",
    "    {\n",
    "        var product = new ProductDataModel\n",
    "        {\n",
    "            Key = Guid.NewGuid().ToString(),\n",
    "            Name = record.Name,\n",
    "            Content = record.Content,\n",
    "            Embedding = await textEmbeddingGeneration.GenerateEmbeddingAsync((string)record.Name + \"\" + (string)record.Content)\n",
    "        };\n",
    "        await recordCollection.UpsertAsync(product);\n",
    "    }\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "python"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
